package com.lanyou.cook.camel.service.impl;

//import java.util.Date;
//import java.util.UUID;
//
//import com.lanyou.cook.camel.commons.Constants;
import com.lanyou.cook.camel.dao.impl.AccessLogDao;
//import com.lanyou.cook.camel.datadictionary.AuditStatusType;
//import com.lanyou.cook.camel.datadictionary.IfEnableType;
//import com.lanyou.cook.camel.datadictionary.IfStatusType;
//import com.lanyou.cook.camel.datadictionary.TransportResultType;
//import com.lanyou.cook.camel.entity.AccessLog;
import com.lanyou.cook.camel.entity.Ftp;
import com.lanyou.cook.camel.exception.AuthException;
import com.lanyou.cook.camel.service.AuthService;
import com.lanyou.cook.camel.service.FtpService;
import com.lanyou.cook.camel.util.CamelFactory;

public class AuthServiceImpl implements AuthService {

	private FtpService ftpService;

	private AccessLogDao accessLogDao;

	public AuthServiceImpl() {
		super();
	}

	public AuthServiceImpl(FtpService ftpService, AccessLogDao accessLogDao) {
		this.ftpService = ftpService;
		this.accessLogDao = accessLogDao;
	}

	@Override
	public void authResult(String interfaceName, String sourceIps,
			String targetIps, String fileName, String fileSize)
			throws AuthException {
//		Ftp ftp = ftpService.getFtpByCode(interfaceName, sourceIps, targetIps,
//				fileName, fileSize);
//		AccessLog accessLog = new AccessLog();
//		accessLog.setId(UUID.randomUUID().toString());
//		if (ftp == null) {
//			accessLog.setInterfaceId("0");
//		} else {
//			accessLog.setInterfaceId(ftp.getId());
//		}
//		accessLog.setSourceIps(sourceIps);
//		accessLog.setTargetIps(targetIps);
//		System.out.println(fileName + ":" + fileSize);
//		accessLog.setFileName(fileName);
//		accessLog.setFileSize(fileSize);
//		accessLog.setTransportTime(new Date());
//		accessLog.setIsEnable(IfEnableType.ENABLE.ValueCode);
//		accessLog.setCreator("SYSTEM");
//		accessLog.setModifier("SYSTEM");
//		accessLog.setCreatedDate(new Date());
//		accessLog.setLastUpdatedDate(new Date());
//		if (ftp == null) {
//			// 接口不存在
//			accessLog.setTransportResult(TransportResultType.FAILD.ValueCode);
//			accessLog.setTransportResutlDes(Constants.MSG_INTERFACE_NOTFOUND);
//			accessLogDao.save(accessLog);
//			// notifyCamel(ftp);
//			throw new AuthException(Constants.MSG_INTERFACE_NOTFOUND,
//					Constants.CODE_INTERFACE_NOTFOUND);
//		}
//
//		if (IfEnableType.DISABLE.ValueCode.equals(ftp.getIsEnable())) {
//			// 接口已被卸载
//			accessLog.setTransportResult(TransportResultType.FAILD.ValueCode);
//			accessLog.setTransportResutlDes(Constants.MSG_INTERFACE_UNINSTALL);
//			accessLogDao.save(accessLog);
//			// notifyCamel(ftp);
//			throw new AuthException(Constants.MSG_INTERFACE_UNINSTALL,
//					Constants.CODE_INTERFACE_UNINSTALL);
//		}
//
//		if (IfStatusType.STOP.ValueCode.equals(ftp.getStatus())) {
//			// 接口已被停用
//			accessLog.setTransportResult(TransportResultType.FAILD.ValueCode);
//			accessLog.setTransportResutlDes(Constants.MSG_INTERFACE_STOP);
//			accessLogDao.save(accessLog);
//			// notifyCamel(ftp);
//			throw new AuthException(Constants.MSG_INTERFACE_STOP,
//					Constants.CODE_INTERFACE_STOP);
//		}
//
//		if (AuditStatusType.UNAUDITED.ValueCode.equals(ftp.getAuditStatus())) {
//			// 接口未审核
//			accessLog.setTransportResult(TransportResultType.FAILD.ValueCode);
//			accessLog.setTransportResutlDes(Constants.MSG_INTERFACE_UNAUDITED);
//			accessLogDao.save(accessLog);
//			// notifyCamel(ftp);
//			throw new AuthException(Constants.MSG_INTERFACE_UNAUDITED,
//					Constants.CODE_INTERFACE_UNAUDITED);
//		}
		// String[] ftpIpPort= targetIps.split(":");
		// System.out.println("IP:---"+ftpIpPort[0]+";--port:---"+ftpIpPort[1]);
		// Socket controlSock = null;
		// try {
		// controlSock = new Socket(ftpIpPort[0],
		// Integer.parseInt(ftpIpPort[1]));
		// } catch (NumberFormatException e) {
		// System.out.println("数据转换异常");
		// } catch (UnknownHostException e) {
		// System.out.println("连接异常");
		// } catch (IOException e) {
		// System.out.println("IO异常");
		// }
		// if(controlSock == null){
		// System.out.println("ftp:---" + ftpIpPort[0]
		// +"--port:---"+ftpIpPort[1]+"连接失败");
		// throw new AuthException(Constants.MSG_INTERFACE_UNAUDITED,
		// Constants.CODE_INTERFACE_UNAUDITED);
		// }

	}

	public void notifyCamel(Ftp ftp) {
		synchronized (CamelFactory.getCamel(ftp.getCode())) {
			CamelFactory.getCamel(ftp.getCode()).notify();
		}
	}

	public FtpService getFtpService() {
		return ftpService;
	}

	public void setFtpService(FtpService ftpService) {
		this.ftpService = ftpService;
	}

	public AccessLogDao getAccessLogDao() {
		return accessLogDao;
	}

	public void setAccessLogDao(AccessLogDao accessLogDao) {
		this.accessLogDao = accessLogDao;
	}

}
